<?php
/**
 * 分销用户自己小店的商品
 * Date: 2020年2月27日
 * Time: 上午10:29:11
 */

namespace App\Model\BbcModel;

use App\Model\BaseModel\BaseBbcModel;
use Hyperf\DbConnection\Db;

class MnStoreGoods extends BaseBbcModel {
    public $table = 'ly_mn_store_goods';

    /**
     * 根据goods_id获取利润额 零售价和会员价
     */
    public static function getGoodsAllProfit($mn_sid, array $goods_id = [], $baseSpec = 1, $level = 0, $spec_num = 0) {
        if (!$goods_id) {
            return [];
        }
        $mnStoreGoodsAll = self::where('mn_sid', (int)$mn_sid)->where('df', 0)->whereIn('goods_id', $goods_id)->get()->toArray();
        if (!$mnStoreGoodsAll) {
            return [];
        }
        $goodsFields = ['id', 'price_recommend', 'price_recommend_vip', 'price_store', 'tax_rate', 'tax_free', 'price_market', 'price_cost', 'is_trade'];
        $goodsAll    = Goods::whereIn('id', $goods_id)->select($goodsFields)->get()->toArray();
        if ($goodsAll) {
            $goodsAll = array_column($goodsAll, null, 'id');
        }
        if ($spec_num == 0) {
            $spec_num = $baseSpec;
        }
        $specPriceAll = GoodsSpecPrice::whereIn('goods_id', $goods_id)->where('spec_num', $spec_num)->get()->toArray();
        if ($specPriceAll) {
            $specPriceAll = array_column($specPriceAll, null, 'goods_id');
        }
        $levelPriceAll = [];
        if ($level > 0) {
            $levelPriceAll = DistributorPrice::where('level_id', $level)->whereIn('goods_id', $goods_id)->where('spec_num', $spec_num)->get()->toArray();
            if ($levelPriceAll) {
                $levelPriceAll = array_column($levelPriceAll, null, 'goods_id');
            }
        }
        $mnSpecPriceAll = MnSpecPrice::where('mn_sid', $mn_sid)->whereIn('goods_id', $goods_id)->where('spec_num', $spec_num)->get()->toArray();
        if ($mnSpecPriceAll) {
            $mnSpecPriceAll = array_column($mnSpecPriceAll, null, 'goods_id');
        }
        $result = [];
        foreach ($mnStoreGoodsAll as $key => $value) {
            $goods      = $goodsAll[$value['goods_id']] ?? [];
            $specPrice  = $specPriceAll[$value['goods_id']] ?? [];
            $levelPrice = $levelPriceAll[$value['goods_id']] ?? [];
            if (!$goods) {
                $result[$value['goods_id']] = ['profit' => 0, 'profit_vip' => 0, 'status' => 0,];
                continue;
            }
            if ($specPrice) {
                $goods['price_cost']          = $specPrice['price_cost'];
                $goods['price_store']         = $specPrice['price_store'];
                $goods['price_recommend']     = $specPrice['price_recommend'];
                $goods['price_recommend_vip'] = $specPrice['price_recommend_vip'];
                $goods['price_market']        = $specPrice['price_market'];
            }
            if ($levelPrice) {
                $goods['price_store'] = $levelPrice['price'];
            }
            if (empty($value) || $value['type'] == 2) {
                $value['profit']     = $goods['price_recommend'] - $goods['price_store'];
                $value['profit_vip'] = $goods['price_recommend_vip'] - $goods['price_store'];
            } elseif ($value['type'] == 1) {
                $value['profit']     = $goods['price_store'] * $value['profit'] / 100;
                $value['profit_vip'] = $goods['price_store'] * $value['profit_vip'] / 100;
            } elseif ($value['type'] == 3) {
                $mnSpecPrice = $mnSpecPriceAll[$value['goods_id']] ?? [];
                if ($mnSpecPrice) {
                    $value['price']     = $mnSpecPrice['price'];
                    $value['price_vip'] = $mnSpecPrice['price_vip'];
                } else {
                    if ($goods['is_trade'] == 0) {
                        $value['price']     = $goods['price_recommend'];
                        $value['price_vip'] = $goods['price_recommend_vip'];
                    }
                }
                $value['profit']     = $value['price'] - $goods['price_store'];
                $value['profit_vip'] = $value['price_vip'] - $goods['price_store'];
            }
            if ($value['profit'] <= 0) {
                $value['profit'] = 0;
            }
            if ($value['profit_vip'] <= 0) {
                $value['profit_vip'] = $value['profit'];
            }
            $value['profit']              = round($value['profit'], 2);
            $value['profit_vip']          = round($value['profit_vip'], 2);
            $value['price_store']         = $goods['price_store'];
            $value['tax_rate']            = $goods['tax_rate'];
            $value['tax_free']            = $goods['tax_free'];
            $value['price_market']        = $goods['price_market'];
            $value['price_recommend']     = $goods['price_recommend'];
            $value['price_recommend_vip'] = $goods['price_recommend_vip'];
            $value['price_cost']          = $goods['price_cost'];

            $result[$value['goods_id']] = $value;
        }
        return $result;
    }


    /**
     * 判断商品是否在店铺内，如果不在则上架，如果下架则上架，主要用于店铺模板设置
     */
    public function checkGoods($param){
        if(empty($param['goods_id'])){
            return true;
        }
        if(!isset($param['mid']) || empty($param['mid'])){
            return true;
        }
        $ifDown=Goods::instance()->info(['id'=>$param['goods_id'],'df'=>0,'status'=>1]);
        if(empty($ifDown)){
            return true;
        }
        $goods=MnStoreGoods::instance()->info(['mn_sid'=>$param['mn_sid'],'goods_id'=>$param['goods_id']]);
        $mnUser=Mnuser::instance()->info(['id'=>$param['mn_sid']]);
        if($ifDown['is_trade']==1 && $mnUser['type']==1){
            return true;
        }

        if($mnUser['type'] == 3){
            if($mnUser['supplier_id'] != $ifDown['supplier_id'] && $ifDown['is_fast_delivery'] != 0){
                return true;
            }
        }
        if(empty($goods) || $goods['status']==0){
            if(empty($goods)){
                $mnGoodsId=MnStoreGoods::instance()->add(['mn_sid'=>$param['mn_sid'],'goods_id'=>$param['goods_id'],'status'=>1,'cd'=>time(),'type'=>2,'mnid'=>2,'mid'=>$param['mid'],'md'=>time()]);
            }else{
                MnStoreGoods::instance()->edit(['id'=>$goods['id']],['status'=>1,'df'=>0,'md'=>time()]);
                $mnGoodsId=$goods['id'];
            }
            //更新索引
            $goods=Goods::instance()->info(['id'=>$param['goods_id'],'_fields_'=>'id,name,class_id,brand_id']);
            $insertItem['id']=$mnGoodsId;
            $insertItem['goodsId']=$param['goods_id'];
            $insertItem['name']=$goods['name'];
            $insertItem['classifyID']=$goods['class_id'];
            $insertItem['storeID']=$param['mn_sid'];
            $insertItem['brandName']=Db::table('ly_goods_brand')->where('id',$goods['brand_id'])->value('name');
            $insertItem['isActive']=true;
            $insertData[]=$insertItem;
            //todo 添加队列
            // Rbq::instance()->pushNew($insertData,'store','insert');
        }
        return true;
    }

}
